// getWechatEnvironment.js

/**
 * 检测微信浏览器环境
 * @returns {Object} 包含环境信息的对象
 */
function getWechatEnvironment() {
  const userAgent = navigator.userAgent.toLowerCase();
  
  // 判断是否为微信浏览器
  const isWechat = userAgent.includes('micromessenger');
  
  if (!isWechat) {
    return {
      isWechat: false,
      isMobile: false,
      isDesktop: false,
      isWechatMobile: false,
      isWechatDesktop: false,
      environment: 'unknown',
      userAgent: userAgent
    };
  }
  
  // 判断是否为微信桌面版 (Windows WeChat)
  const isWechatDesktop = userAgent.includes('windowswechat');
  
  // 判断是否为移动端设备
  const isMobile = /android|webos|iphone|ipad|ipod|blackberry|iemobile|opera mini/i.test(userAgent);
  
  // 如果是微信环境但不是桌面版，则认为是移动端
  const isWechatMobile = isWechat && !isWechatDesktop;
  
  // 返回环境信息对象
  return {
    isWechat: true,
    isMobile: isMobile,
    isDesktop: !isMobile,
    isWechatMobile: isWechatMobile,
    isWechatDesktop: isWechatDesktop,
    environment: isWechatDesktop ? 'wechat-desktop' : isWechatMobile ? 'wechat-mobile' : 'unknown-wechat',
    userAgent: userAgent
  };
}

// 以对象形式导出
export default {
  getWechatEnvironment,
  
  // 也可以直接导出环境检测结果
  environmentInfo: getWechatEnvironment(),
  
  // 提供一些常用的环境判断常量
  isWechat: getWechatEnvironment().isWechat,
  isWechatMobile: getWechatEnvironment().isWechatMobile,
  isWechatDesktop: getWechatEnvironment().isWechatDesktop,
  isMobile: getWechatEnvironment().isMobile,
  isDesktop: getWechatEnvironment().isDesktop
};